package com.zut.controller;

import com.github.pagehelper.PageInfo;
import com.zut.domain.ExamResult;
import com.zut.service.ExamResultService;
import org.apache.ibatis.annotations.Delete;
import org.apache.ibatis.annotations.Select;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Map;

@CrossOrigin
@RestController
@RequestMapping("/join")
public class ExamResultController {

    @Autowired
    private ExamResultService examResultService;

    /*得到所有的考试信息*/
    @GetMapping("/{pageNum}/{pageSize}")
    public Result getAll(@PathVariable Integer pageNum, @PathVariable Integer pageSize) {
        PageInfo<ExamResult> examResultPageInfo = examResultService.getAll(pageNum, pageSize);
        Integer code = examResultPageInfo.getSize() != 0 ? Code.GET_OK : Code.GET_ERR;
        String msg = examResultPageInfo.getSize() != 0 ? "" : "数据查询失败，请重试！";
        return new Result(code, examResultPageInfo, msg);
    }

    /*预约考试，不要插入成绩*/
    @PostMapping
    public Result insert(@RequestBody ExamResult examResult) {
        boolean flag = examResultService.insert(examResult);
        return new Result(flag ? Code.SAVE_OK : Code.SAVE_ERR, flag);
    }

    /*根据用户id，考试id删除考试结果信息*/
    @DeleteMapping("/{userId}/{examId}")
    public Result deleteById(@PathVariable String userId, @PathVariable String examId) {
        boolean flag = examResultService.deleteById(userId, examId);
        return new Result(flag ? Code.DELETE_OK : Code.DELETE_ERR, flag);
    }

    /*修改考试结果信息*/
    @PutMapping
    public Result Update(@RequestBody ExamResult examResult) {
        boolean flag = examResultService.update(examResult);
        return new Result(flag ? Code.UPDATE_OK : Code.UPDATE_ERR, flag);
    }

    /*根据用户id查询成绩单*/
    @GetMapping("/{pageNum}/{pageSize}/{id}")
    public Result getByPlace(@PathVariable Integer pageNum, @PathVariable Integer pageSize, @PathVariable String id) {
        PageInfo<ExamResult> examResultPageInfo = examResultService.getById(pageNum, pageSize, id);
        Integer code = examResultPageInfo.getSize() != 0 ? Code.GET_OK : Code.GET_ERR;
        String msg = examResultPageInfo.getSize() != 0 ? "" : "数据查询失败，请重试！";
        return new Result(code, examResultPageInfo, msg);
    }

    /*查询用户预约考试状态*/
    @GetMapping("/user/{pageNum}/{pageSize}")
    public Result getExamStatus(@PathVariable Integer pageNum, @PathVariable Integer pageSize) {
        PageInfo<Map<String, String>> examStatus = examResultService.getExamStatus(pageNum, pageSize);
        Integer code = examStatus.getSize() != 0 ? Code.GET_OK : Code.GET_ERR;
        String msg = examStatus.getSize() != 0 ? "" : "数据查询失败，请重试！";
        return new Result(code, examStatus, msg);
    }

    /*查询已预约用户信息*/
    @GetMapping("/booked/{pageNum}/{pageSize}")
    public Result getExamStatusIs(@PathVariable Integer pageNum, @PathVariable Integer pageSize) {
        PageInfo<ExamResult> examStatus = examResultService.getExamStatusIs(pageNum, pageSize);
        Integer code = examStatus.getSize() != 0 ? Code.GET_OK : Code.GET_ERR;
        String msg = examStatus.getSize() != 0 ? "" : "数据查询失败，请重试！";
        return new Result(code, examStatus, msg);
    }
}
